package com.example.administrator.demo1;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import Utils.L;

import static android.os.Build.VERSION_CODES.M;

public class MainActivity extends AppCompatActivity {

    private static final int AUTO_SCROLL = 0;
    private ViewPager mViewpager;
    private TextView mTv;
    private int[] img;
    private String[] notes;
    private LinearLayout mDotLinear;
    private GridView mGridView;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case AUTO_SCROLL:
                    //自动播放图片
                    int currentItem = mViewpager.getCurrentItem();
                    mViewpager.setCurrentItem(currentItem++);
                    break;

            }
        }
    };
    private Runnable run;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = new int[]{R.drawable.add, R.drawable.music, R.drawable.wireless};
        notes = new String[]{"添加添加添加", "音乐音乐音乐", "新号信号信号"};

        mViewpager = (ViewPager) findViewById(R.id.main_viewpager);
        mDotLinear = (LinearLayout) findViewById(R.id.linear_dot);
        mGridView = (GridView) findViewById(R.id.main_gridview);
        mTv = (TextView) findViewById(R.id.ll_tv);

        mTv.setText(notes[0]);
        mDotLinear.getChildAt(0).setSelected(true);

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
        mViewpager.setAdapter(myPagerAdapter);
        MyPageChange myPageChange = new MyPageChange();
        mViewpager.setOnPageChangeListener(myPageChange);

        MyGridAdapter myGridAdapter = new MyGridAdapter();
        mGridView.setAdapter(myGridAdapter);

        //自动播放图片
        run = new Runnable() {
             @Override
             public void run() {
                 //自动播放图片
                 int currentItem = mViewpager.getCurrentItem();
                 if (currentItem + 1 >= img.length) {
                     currentItem = -1;
                 }
                 mViewpager.setCurrentItem(++currentItem);
                 mHandler.postDelayed(this, 1500);
             }
         };
        mHandler.postDelayed(run, 1500);
    }


    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return img.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = new ImageView(MainActivity.this);
            iv.setImageResource(img[position]);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            L.i(position + "");
//            container.removeViewAt(position);
        }
    }

    class MyPageChange implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mHandler.removeCallbacks(run);
            mHandler.postDelayed(run, 1500);
            mTv.setText(notes[position]);
            mDotLinear.getChildAt(position).setSelected(true);
            int childCount = mDotLinear.getChildCount();
            for (int i = 0; i < childCount; i++) {
                if (i != position) {
                    mDotLinear.getChildAt(i).setSelected(false);
                }
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
//            L.i(state+"           滑动状态");//滑动  无动作  释放
        }
    }

    class MyGridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return img.length;
        }

        @Override
        public Object getItem(int position) {
            return img[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(MainActivity.this);
            iv.setImageResource(R.drawable.music);
            LinearLayout.LayoutParams iLP = (LinearLayout.LayoutParams) iv.getLayoutParams();
            iLP.height += 40;
            iLP.width += 40;

            iv.setLayoutParams(iLP);
            return iv;
        }
    }

    class viewHolder{

    }
}
